package com.java46.struts2.Food.dao;


import com.java46.struts2.base.entity.TDishesEntity;
import com.java46.struts2.base.exception.BusinessException;
import com.java46.struts2.base.util.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;
/**
 * @ClassName FoodDao
 * @Description 菜品的CRUD
 * @Author Tigger Wu
 * @Date 2018/7/16 19:33
 * @Version 1.0
 **/
public class FoodDao {
    /*
     *  查询全部菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 11:56
     * @param
     * @return
     */

    public  List<TDishesEntity> queryAll(){
        List<TDishesEntity>dlist = null;
        try {
            Query query = HibernateUtils.getQuery("from TDishesEntity");
            dlist = query.list();
        }catch (Exception e){
            throw  new BusinessException("查询失败！");
        }finally {
        }
        return dlist;
    }
    /*
     *  添加菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 11:57
     * @param
     * @return
     */
        public  void addFood(TDishesEntity food){
        try {
            Session session = HibernateUtils.getSession();
            Transaction transaction = session.beginTransaction();
            session.save(food);
            transaction.commit();
            session.close();
        }catch (Exception e){
            throw  new BusinessException("sadas");
             }
        }
/*
 * 删除菜品
 *
 * @author Tigger Wu
 * @date 2018/7/17 14:24
 * @param [food]
 * @return void
 */
    public  void removeFoods(int id){
                try {
                    Session session = HibernateUtils.getSession();
                    Transaction tran = session.beginTransaction();
                    TDishesEntity foods = session.get(TDishesEntity.class,id);
                    session.delete(foods);
                    tran.commit();
                    session.close();
                }catch (Exception e){
                    throw  new BusinessException("gg");
                }
    }
/*
 * 修改菜品
 *
 * @author Tigger Wu
 * @date 2018/7/17 14:32
 * @param
 * @return
 */
public void  updateFoods(TDishesEntity foods){
    try {
        Session session = HibernateUtils.getSession();
        Transaction tran = session.beginTransaction();
        TDishesEntity food = session.get(TDishesEntity.class,foods.getdId());
        food.setdId(foods.getdId());
        food.setdImg(foods.getdImg());
        food.setdName(foods.getdName());
        food.setdPrice(foods.getdPrice());
        food.setdRemark(foods.getdRemark());
        session.update(food);
        tran.commit();
        session.close();
    }catch (Exception e){
        throw  new BusinessException("uu");
    }

}
/**
 * 根据类型Id查询该类型所有菜品
 *
 * @author TiggerWu
 * @date 2018/7/18  10:37
 * @param
 * @return
 */
    public Integer[] queryFoodsId(int typeId){

        Integer [] arr =null;
        try {
            String hql = "from TDishesEntity f where dTypeId="+typeId;
            Session session = HibernateUtils.getSession();
            Query query = session.createQuery(hql);
            List<TDishesEntity> foods  = query.list();
            arr = new Integer[foods.size()];
            for(int i = 0 ;i<foods.size();i++){
                arr[i]= foods.get(i).getdId();

            }
        }catch (Exception e){
            throw new BusinessException("oo");
        }
        return arr;
    }
    /**
     *  根据id查询菜品
     *
     * @author TiggerWu
     * @date 2018/7/18  17:17
     * @param
     * @return
     */
    public List<TDishesEntity> queryById(int id){
        List<TDishesEntity> foods = null;
        try {
            String hql = "from TDishesEntity f where dId="+id;
            Session session = HibernateUtils.getSession();
            Query query = session.createQuery(hql);
            foods  = query.list();
            session.close();
        }catch (Exception e){
            throw new BusinessException("x",e);
        }
        return foods;
    }
 }

